import random

from plotting.drawer import plot_gantt_chart, draw_precedence_relations
from data_parsers.parser_fjsp import parse_fjsp
from scheduling_environment.jobShop import JobShop

from solution_methods.helper_functions import load_job_shop_env, load_parameters
from solution_methods.dispatching_rules.run_dispatching_rules import run_dispatching_rules
from solution_methods.GA.src.initialization import initialize_run
from solution_methods.GA.run_GA import run_GA
from solution_methods.MILP.run_MILP import run_MILP
from solution_methods.FJSP_DRL.run_FJSP_DRL import run_FJSP_DRL

#
# jobShopEnv = JobShop()
# jobShopEnv = parse_fjsp(jobShopEnv, '/fjsp/brandimarte/MK01.fjs')
#
# jobShopEnv = load_job_shop_env('/fjsp/brandimarte/MK01.fjs')
#
# draw_precedence_relations(jobShopEnv)
#
#
# # Display the Job Shop Environment information
# print('作业车间环境：/Job Shop Environment:')
# print(jobShopEnv)
#
#
# # Display the list of jobs in the environment
# print('作业如下：/With the following Jobs:')
# print(jobShopEnv.jobs)
#
#
# # Display the operations of the first job (job_id=0)
# print('Where the first job (job_id=0) has the following operations:')
# job = jobShopEnv.get_job(job_id=0)
# print(job.operations)

if __name__ == '__main__':
    from multiprocessing import freeze_support
    freeze_support()

    jobShopEnv = load_job_shop_env('/fjsp/brandimarte/MK04.fjs')
    jobShopEnv.update_operations_available_for_scheduling()
    while len(jobShopEnv.scheduled_operations) < jobShopEnv.nr_of_operations:
        operation = random.choice(jobShopEnv.operations_available_for_scheduling)
        machine_id = random.choice(list(operation.processing_times.keys()))
        duration = operation.processing_times[machine_id]
        jobShopEnv.schedule_operation_on_machine(operation, machine_id, duration)
        jobShopEnv.update_operations_available_for_scheduling()

    plot_gantt_chart(jobShopEnv)

    # 调度规则
    parameters = load_parameters("configs/dispatching_rules.toml")
    jobShopEnv = load_job_shop_env(parameters['instance'].get('problem_instance'))

    makespan, jobShopEnv = run_dispatching_rules(jobShopEnv, **parameters)
    plot_gantt_chart(jobShopEnv)

    print("调度规则，最小化最大完工时间——makespan:"+str(makespan))


    # 遗传算法
    parameters = load_parameters("configs/GA.toml")
    jobShopEnv = load_job_shop_env(parameters['instance'].get('problem_instance'))

    population, toolbox, stats, hof = initialize_run(jobShopEnv, **parameters)
    makespan, jobShopEnv = run_GA(jobShopEnv, population, toolbox, stats, hof, **parameters)

    print("GA遗传算法，最小化最大完工时间——makespan:"+str(makespan))

    plot_gantt_chart(jobShopEnv)

    #
    # # FJSP_DRL 深度强化学习
    # parameters = load_parameters("configs/fjsp_drl.toml")
    # jobShopEnv = load_job_shop_env(parameters['test_parameters'].get('problem_instance'))
    #
    # makespan, jobShopEnv = run_FJSP_DRL(jobShopEnv, **parameters)
    #
    # print("DRL深度强化学习，最小化最大完工时间——makespan:"+str(makespan))

    # plot_gantt_chart(jobShopEnv)